<?php
/**
* @package   auto_occasion
* @subpackage formule
* @author    your name
* @copyright 2011 your name
* @link      http://www.yourwebsite.undefined
* @license    All rights reserved
*/

class tableauZone extends jZone {
    protected $_tplname='tableau.zone';

    protected function _prepareTpl(){
        
        jClasses::inc("essais~techniqueSrv");
        $essaiId = $this->param('id');
        $tableau = techniqueSrv::getAllByEssaiId($essaiId) ;

        // Regrouper les éléments par caractéristique afin de faciliter la boucle
        $version = $newTab = array();
        $i = 0 ;
        foreach ($tableau as $k=>$item) {
            $version[$item->id_version]  = $item->version_name;
            $desc[$item->id_version]     = $item->description;
            if ((isset($tableau[$k+1]) && $tableau[$k+1]->id_caracteristique != $item->id_caracteristique) || !isset($tableau[$k+1])) {
                
                $caracteristique = new stdClass();
                $caracteristique->caracter_name = $item->caracter_name;
                $caracteristique->desc = $desc;
                if(isset($newTab[$i-1]) && ((($newTab[$i-1]->caracter_name) == ($item->caracter_name)) || (strlen($newTab[$i-1]->caracter_name) == 0))){
                    foreach ($caracteristique->desc as $key => $value) {
                        $newTab[$i-1]->desc[$key]     = $value;
                    }                    
                }
                else{
                    $newTab[$i] = $caracteristique;
                }
                $i++;
                unset($desc);
            }
        }

        
        // test si au cas ou un élément n'a pas de version
       $tempDesc = $tempDescItem = array();

        foreach ($newTab as $tab) {
            foreach ($version as $k=>$itemVersion){
                    
                    if(!array_key_exists($k, $tab->desc)) {
                        $tempDescItem[$k] = "";
                    } else {
                        $tempDescItem[$k] = $tab->desc[$k]; 
                    }
            }
            $tab->desc = $tempDescItem;
            $tempDesc[] = $tab;
            unset($tempDescItem);
        }

        $this->_tpl->assign('version', $version);
        $this->_tpl->assign('tableau', $tempDesc);
    }
}
